// components/home/remove/index.js
import {
  remove
} from '../../../api/collection_record'
import {
  useDialog
} from '../../../miniprogram_npm/miniprogram-vc-components/dialog/useDialog';

let dialog;
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    dataSource: {
      type: Object,
      value: {},
      observer(data) {
        this.setData({
          selected: this.properties.ids.includes(data.id)
        })
      }
    },
    selectState: {
      type: Boolean,
      value: false

    },
    ids: {
      type: Array,
      value: [],
      observer(data) {
        this.setData({
          selected: data.includes(this.properties.dataSource.id)
        })
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    selected: false,
    root: wx.getStorageSync('root')
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onRemove() {

      // remove(this.properties.dataSource, () => {
      //   this.triggerEvent('remove', this.properties.dataSource.id)
      // })
      wx.showModal({

        content: "确定要删除吗？",
        success: res => {
          if (res.confirm) {
            remove(this.properties.dataSource, () => {
              this.triggerEvent('remove', this.properties.dataSource.id)
            })
          }
        }
      })
    },
    none() {
      return false
    },
    onSelectToggle() {
      const {
        dataSource
      } = this.properties;
      if (dataSource.status === 1) return
      let ids = JSON.parse(JSON.stringify(this.properties.ids));

      if (ids.includes(dataSource.id)) {
        ids = ids.filter(it => it !== dataSource.id);
      } else {
        ids.push(dataSource.id);
      }
      this.triggerEvent('selected', ids);
    }
  },
  lifetimes: {
    attached: function () {
      dialog = useDialog('#dialog', this);
    }
  }
})